<?php
global $_W, $_GPC;

$videoTable = 'hellozjx_mapwxapp_video';
$subjectTable = 'hellozjx_mapwxapp_subject';
$classRoomTable = 'hellozjx_mapwxapp_classroom';
$teacherTable = 'hellozjx_mapwxapp_teacher';
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';

if ($operation == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $keyword = trim($_GPC['keyword']);
    $condition = '';
    if (!empty($keyword)) {
        $condition = " AND v.name LIKE '%{$keyword}%'";
    }
    $list = pdo_fetchall("select v.*,m.name as m_name,t.name as t_name from ".tablename($videoTable).' v'.' left join '.tablename($teacherTable).' t on t.id=v.teacher_id'.' left join '.tablename($classRoomTable).' m on m.id=v.room_id '."where v.status>0 {$condition} ORDER BY v.sort DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($videoTable) .' v'. " WHERE v.status>0 " . $condition);
    $pager = pagination($total, $pindex, $psize);
} elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    if (checksubmit('submit')) {
        $data = array(
            'uniacid' => $_W['uniacid'],
            'teacher_id' =>intval($_GPC['teacher_id']),
            'course_id' => intval($_GPC['course_id']),
            'room_id' => intval($_GPC['room_id']),
            'name' => $_GPC['name'],
            'is_charge' => $_GPC['is_charge'],
            'price' => $_GPC['price'],
            'img_url' => $_GPC['img_url'],
            'video_url' => $_GPC['video_url'],
            'status' => $_GPC['status'],
            'sort' => $_GPC['sort'],
            'create_time' => date('Y-m-d H:i:s',time())
        );
        if($td_id>0)
        {
            $data['td_url']=$_GPC['td_url'];
        }
        $where = '';
        if(!empty($id))
            $where = ' AND id<>'.$id;
        $isExists = pdo_fetch("select id from ". tablename($videoTable) ." where name='{$data['name']}' {$where}");
        if($isExists)
            message('操作失败，已存在同名视频！', $this->createWebUrl('video', array('op' => 'display')), 'error');
        if (!empty($id)) {
            unset($data['createtime']);
            pdo_update($videoTable, $data, array('id' => $id));
        } else {
            pdo_insert($videoTable, $data);
            $id = pdo_insertid();
        }
        message('更新信息成功！', $this->createWebUrl('video', array('op' => 'display')), 'success');
    }

    $teacher = pdo_fetchall("select id,name from " . tablename($teacherTable) . " where status =1"); //所有老师
    $subject = pdo_fetchall("select id,name from " . tablename($subjectTable). " where status=1"); //查找所有课程
    $class =  pdo_fetchall("select id,name from " . tablename($classRoomTable)); //查找所有课堂
    $video = pdo_fetch("select v.*,s.name as s_name,s.id as s_id,m.name as m_name,m.id as m_id,t.name as t_name,t.id as t_id from ".tablename($videoTable).' v'.' left join '.tablename($subjectTable).' s on s.id=v.course_id'.' left join '.tablename($teacherTable).' t on t.id=v.teacher_id'.' left join '.tablename($classRoomTable).' m on m.id=v.room_id '."where v.id=:id and v.uniacid=:uniacid limit 1", array(":id" => $id, ":uniacid" => $_W['uniacid']));
}elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $video = pdo_fetch("SELECT id FROM " . tablename($videoTable) . " WHERE id = '{$id}' AND status > 0");
    if (empty($video)) {
        message('抱歉，视频不存在或是已经被删除！', $this->createWebUrl('video', array('op' => 'display')), 'error');
    }
    pdo_update($videoTable, ['status'=>0], array('id' => $id));
    message('删除成功', referer(), 'success');
}
include $this->template('video');